package com.example.comment.mq.config;

import org.hibernate.validator.internal.util.logging.Log_$logger;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import javax.validation.Valid;

import static org.springframework.amqp.core.QueueBuilder.durable;

/**
 * @author QingZeyu
 * @date 2024/5/9 11:37
 */
@Configuration
public class MqConfig {
    @Value("${mq-comment.exchange}")
    private static final String EXCHANGE_NAME = "commentExchange";

    @Value("${mq-comment.routingKey}")
    private static final String ROUTING_KEY = "commentRoutingKey";

    @Value("${mq-comment.queue}")
    private static final String QUEUE_NAME = "commentQueue";

    @Bean(EXCHANGE_NAME)
    public Exchange exchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME)
                .durable(true)
                .build();
    }

    @Bean(QUEUE_NAME)
    public Queue commentQueue(){
        return durable(QUEUE_NAME)
                .withArgument("x-dead-letter-exchange", EXCHANGE_NAME)
                .withArgument("x-dead-letter-routing-key", ROUTING_KEY)
                .build();
    }

    @Bean
    public Binding bindingComment(@Qualifier(QUEUE_NAME) Queue queue,@Qualifier(EXCHANGE_NAME) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY).noargs();
    }
}
